package com.foodndate.food.thema;

import java.lang.ProcessBuilder.Redirect;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

@Controller
@RequestMapping("/thema")
public class ThemaController {

	@Autowired private ThemaDao dao;
	
	//글쓰기 폼으로 이동
	@RequestMapping(value="/page")
	public String page(HttpSession session){
		session.setAttribute("sessionId","test");
		return "thema/themaInsert";
	}
	
	//글 인설트 
	@RequestMapping(value="/themaInsert", method=RequestMethod.POST)
	public String themaInsert(ThemaDto themaDto){
		dao.themaInsert(themaDto);		
		return "redirect:/thema/themaSelectList?nowPage=1";
	}
	
	//글리스트
	@RequestMapping(value="/themaSelectList", method=RequestMethod.GET)
	public String themaSelectList(Model model,ThemaPageDto pageDto){
		int themaListCount=dao.themaSelectCount();
		if(themaListCount%16>0){
			pageDto.setMaxPage(themaListCount/16+1);
		}else{
			pageDto.setMaxPage(themaListCount/16);
		}
		
		
		if(pageDto.getNowPage()-5>0){
			pageDto.setStartPage(pageDto.getNowPage()-5);
			pageDto.setEndPage(pageDto.getNowPage()+5);				
		}else{
			pageDto.setStartPage(1);
			pageDto.setEndPage(10);
		}
		
		if(pageDto.getMaxPage()<pageDto.getEndPage()){
			pageDto.setEndPage(pageDto.getMaxPage());
		}		
		
		List<ThemaDto>	themaDtoList = dao.themaSelectList(pageDto);
		model.addAttribute("themaDtoList", themaDtoList);
		model.addAttribute("pageDto", pageDto);
		return "/thema/themaList";
	}
	
	//글상세보기
	@RequestMapping(value="/themaDatile", method=RequestMethod.GET)
	public String themaDatile(Model model,ThemaDto themaDto,ThemaPageDto pageDto){
			
		List<ThemaDto>		themaDtoList 	= dao.themaDatile(themaDto);
		List<ThemaOneDto>	themaOneDtoList = dao.themaOneDatile(themaDto);
		
		model.addAttribute("themaDtoList", themaDtoList);
		model.addAttribute("themaOneDtoList", themaOneDtoList);
		model.addAttribute("pageDto", pageDto);
		
		return "/thema/themaDatile";
	}
	
	//한줄평가 등록
	@RequestMapping(value="/theamOneInsert", method=RequestMethod.POST)
	public String theamOneInsert(Model model,ThemaOneDto themaOneDto){
		String msg= dao.themaOneInsert(themaOneDto);
		model.addAttribute("msg", msg);
		return "redirect:/thema/themaDatile?thema_num="+themaOneDto.getThema_num();
	}
}
